package com.yycx.module.bus.provider.handler.haiying.order;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 未审批订单列表
 *
 * @author zyf
 */
@Component
public class HyOrderTab4ListHandler implements PageInterceptor<HyOrder> {


    @Autowired
    private TaskService taskService;
    @Autowired
    private HyOrderService orderService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery<HyOrder> cq, PageParams pageParams, EntityMap params) {
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String deptId = params.get("deptId");
        String orderNo = params.get("orderNo");
        String parentId = params.get("parentId");
        String searchRang = params.get("searchRang","2");
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "ho.sendDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "ho.sendDate", endDate);
        cq.eq(FlymeUtils.isNotEmpty(deptId), "ho.deptId", deptId);
        cq.likeLeft(FlymeUtils.isNotEmpty(parentId), "ho.parentId", parentId);
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        OpenUser openUser = OpenHelper.getUser();
        String userName = openUser.getUsername();
        if (!userName.contains("admin") && searchRang.equals("2")) {
            cq.eq("ho.userId", openUser.getUserId());
        }
        cq.eq(HyOrder.class, "companyId", openUser.getCompanyId());
        //退货通知单
        cq.in("ho.orderType", HySaleTypeEnum.CHC.getCode(),"CKJ");
        cq.in("orderState", 0, 1,2);
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            String parentId = entityMap.get("parentId");
            if (FlymeUtils.isNotEmpty(parentId)) {
                HyOrder parent = orderService.getById(parentId);
                entityMap.put("parentOrderNo", parent.getOrderNo());
            }
            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }

}
